﻿using Genius.Brain.Shared.Strategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Genius.Brain.Shared.BO;
using Genius.Brain.Shared.Locator;

namespace Genius.Brain.Impl.Strategy
{
    class TextTokenizerStrategy : ITextTokenizerStrategy
    {
        public IList<ITextToken> Tokenize(string text)
        {
            var state = 0;
            var list = new List<ITextToken>();
            char[] a = (text + " ").ToCharArray();
            var i = 0;
            var s = 0;
            while(i < a.Length)
            {
                switch(state)
                {
                    case 0:
                        s = i; //StartIndex
                        if (Char.IsWhiteSpace(a[i])) break;
                        if(Char.IsPunctuation(a[i])) 
                        {
                            list.Add(BrainLocator.GetTextToken("mark", a[i].ToString()));
                            break;
                        }

                        if (Char.IsLetter(a[i]))
                        {
                            state = 1; //Word
                            break;
                        }

                        if (Char.IsNumber(a[i]))
                        {
                            state = 2; //Number
                            break;
                        }

                        //error
                        {
                            list.Add(BrainLocator.GetTextToken("error", a[i].ToString()));
                        }
                        break;
                    case 1:
                        if (!Char.IsLetter(a[i]))
                        {
                            list.Add(BrainLocator.GetTextToken("word", text.Substring(s, i - s)));
                            state = 0;
                            continue;
                        }
                        break;
                    case 2:
                        if((a[i] == '.' || a[i] == ',') && Char.IsNumber(LookAHead(a,i+1)))
                        {
                            state = 3; //Decimal Number
                            break;
                        }
                        if (!Char.IsNumber(a[i]))
                        {
                            list.Add(BrainLocator.GetTextToken("number", text.Substring(s, i - s)));
                            state = 0;
                            continue;
                        }
                        break;
                    case 3:
                        if (!Char.IsNumber(a[i]))
                        {
                            list.Add(BrainLocator.GetTextToken("number", text.Substring(s, i - s)));
                            state = 0;
                            continue;
                        }
                        break;
                }

                i++;

            }


            return list;

        }

        private char LookAHead(char[] a, int i)
        {
            if (i < a.Length)
                return a[i];
            return '\0';
        }
    }
}
